/**
 * by ward @ harrydeep@163.com 
 * 2019
 */
package top.wardyan.base.action;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

import top.wardyan.base.config.Const;


/**
 * @author ward
 * 用途:
 * 版本:
 * 创建:
 * 更新:
 */
public abstract class BaseInit<T extends BaseInit<T>> extends Shell{
	
	
	
	
	@SuppressWarnings("unchecked")
	public BaseInit(Integer style) {
		super(style);
		this.self = (T) this;
		this.init();
	}

	
	private static final Integer WINDOW_WIDTH = 600;
	private static final Integer WINDOW_HEIGHT = 200;
	
	protected T self;
	
	//private static String SYS_TITLE = WReport.SYSNAME;
	
	private Timer timer;
	private ProgressBar progressBar;

	@Override
	public void open(){
		super.open();
		this.layout();		
	}
	
	
	
	
	

	
	protected void init() {
		this.setBackground(Const.COLOR_WHITE);
		this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		this.setLocation((Const.SCREENWIDTH - WINDOW_WIDTH) / 2, (Const.SCREENHEIGHT - WINDOW_HEIGHT) / 2);
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setFont(Const.FONT_H3);
		lblNewLabel.setBackground(Const.COLOR_WHITE);
		lblNewLabel.setAlignment(SWT.CENTER);
		lblNewLabel.setBounds(0, 21, 594, 86);
		lblNewLabel.setText("标题");
		
		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBackground(Const.COLOR_WHITE);
		lblNewLabel_1.setAlignment(SWT.CENTER);
		lblNewLabel_1.setFont(Const.FONT_H7);
		lblNewLabel_1.setVisible(true);
		lblNewLabel_1.setBounds(0, 144, 594, 27);
		lblNewLabel_1.setText("加载中...");
		
		progressBar = new ProgressBar(this, SWT.NONE);
		progressBar.setBounds(0, 121, 594, 17);

		timer = new Timer();
		timer.schedule(new TimerTask(){
			@Override
			public void run() {
				Display.getDefault().syncExec(loading);
			}
		}, 100, 100);
	}
	
	private Runnable loading = new Runnable(){
		@Override
		public void run(){
			if(!self.isDisposed()){
				if(progressBar.getSelection() < progressBar.getMaximum())
					progressBar.setSelection(progressBar.getSelection() + 1);
				else
					progressBar.setSelection(progressBar.getMinimum());
			}else{
				timer.cancel();
			}
		}
	};
	
	
	public void close(){
		if(!this.isDisposed())
			this.dispose();
	}
	
	
	protected abstract void load();

	
	public void go() {
		//super.go();
		Const.display.asyncExec(new Runnable(){
			@Override
			public void run(){
				load();
				self.dispose();
			}
		});
		while (!this.isDisposed()) {
			if (!this.getDisplay().readAndDispatch()) {
				this.getDisplay().sleep();
			}
		}
	}

	
	protected String getAppTitle() {
		return "标题";
	}







	@Override
	protected void checkSubclass() {}
	
	

}
